package com.dragon.modules.yuegou.service.impl.live;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.client.wechat_mini.common.MiniConfig;
import com.dragon.modules.yuegou.client.wechat_mini.model.LiveReplay;
import com.dragon.modules.yuegou.client.wechat_mini.request.GetReplayRequest;
import com.dragon.modules.yuegou.client.wechat_mini.response.GetReplayResponse;
import com.dragon.modules.yuegou.client.wechat_mini.service.MiniService;
import com.dragon.modules.yuegou.client.wechat_mini.service.impl.MiniServiceImpl;
import com.dragon.modules.yuegou.entity.live.BroadcastEntity;
import com.dragon.modules.yuegou.entity.live.GetliveinfoEntity;
import com.dragon.modules.yuegou.mapper.live.GetliveinfoMapper;
import com.dragon.modules.yuegou.model.live.Getliveinfo;
import com.dragon.modules.yuegou.model.system.Application;
import com.dragon.modules.yuegou.query.live.BroadcastQuery;
import com.dragon.modules.yuegou.query.live.GetliveinfoQuery;
import com.dragon.modules.yuegou.service.live.BroadcastService;
import com.dragon.modules.yuegou.service.live.GetliveinfoService;
import com.dragon.modules.yuegou.service.system.ApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 直播间回放视频 服务实现
 *
 * @author ZSH
 * @date 2020-06-05 21:01:01
 * @version 2.0
 */
@Service("liveGetliveinfoService")
public class GetliveinfoServiceImpl extends ServiceImpl<GetliveinfoMapper, GetliveinfoEntity>
		implements GetliveinfoService {

	@Autowired
	private BroadcastService broadcastService;

	@Autowired
	private ApplicationService applicationService;

	@Override
	public List<Getliveinfo> replayList(String broadcastId) {
		GetliveinfoQuery query = new GetliveinfoQuery();
		query.setBroadcastId(broadcastId);
		QueryWrapper<GetliveinfoEntity> wrapper = query.toQueryWrapper();
		wrapper.orderByAsc(GetliveinfoEntity.COL_CREATE_TIME);
		List<GetliveinfoEntity> list =list(wrapper);

		return list.stream().map(Getliveinfo::new).collect(Collectors.toList());
	}

	@Override
	public void synGetliveinfo() {
		List<String> busdIds = broadcastService.busdIds();
		if (CollectionUtils.isEmpty(busdIds)) {
			return;
		}
		for (String busdId : busdIds) {
			BroadcastQuery query = new BroadcastQuery();
			query.liveStatus().eq("103");
			query.setBusdId(busdId);
			List<BroadcastEntity> list = broadcastService.list(query.toQueryWrapper());
			// 查询application信息
			Application application = applicationService.getOriginalId(busdId);
			MiniService service = new MiniServiceImpl(
					new MiniConfig(application.getAppid(), application.getAppsecret()));
			for (BroadcastEntity broadcast : list) {
				GetReplayRequest request = new GetReplayRequest(Integer.parseInt(broadcast.getRoomId()), 0, 20);
				GetReplayResponse response = service.live().getReplay(request);
				for (LiveReplay replay : response.getLiveReplay()) {
					Getliveinfo info = new Getliveinfo();
					info.setBroadcastId(broadcast.getRoomId());
					info.setBusdId(busdId);
					info.setCreateTime(datefamat(replay.getCreateTime()));
					info.setExpireTime(datefamat(replay.getExpireTime()));
					info.setMediaUrl(replay.getMediaUrl());
					baseMapper.insert(info);
				}
			}
		}
	}

	private LocalDateTime datefamat(String date) {
		Instant parse = Instant.parse(date);
		ZoneId zone = ZoneId.systemDefault();
		return LocalDateTime.ofInstant(parse, zone);
	}

}